package com.knife.database.backup.concurrent;


import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 1 用于执行接口监控任务的线程池
 *
 * @author: tianhao.luo@hand-china.com 2021/6/30  上午10:40
 */
public class KnifeDbBackThreadPool {

    private static volatile ThreadPoolExecutor threadPoolExecutor;

    public static ThreadPoolExecutor getThreadPoolExecutor() {
        if (threadPoolExecutor == null) {
            synchronized (KnifeDbBackThreadPool.class) {
                // 避免阻塞之后多次创建
                if (threadPoolExecutor == null) {
                    BasicThreadFactory.Builder builder = new BasicThreadFactory.Builder();
                    ThreadFactory threadFactory = builder.namingPattern("KnifeDbBackThreadPool-%d").build();
                    //定义线程工厂,且使用任务无法处理时重新排队的处理
                    threadPoolExecutor = new ThreadPoolExecutor(2,
                            10,
                            10,
                            TimeUnit.SECONDS,
                            new ArrayBlockingQueue<>(2048),
                            threadFactory,
                            new ThreadPoolExecutor.CallerRunsPolicy());
                }
            }
        }
        return threadPoolExecutor;
    }
}
